let fs = require("fs");

let index = {
    index: async function (req, res) {   //url动态资源.js 的req
        let mysql = require("../../model/base");
        let date = req.query['date'];
        //console.log('date-----'+date);
        let sql = 'select * from number where date=?';
        //console.log(res);
        let result= await mysql.query(sql,date,res);
                                                
        console.log(result);   
        let html='';
        for (let val in result) {
            html += '<tr>'
            html += '<td>' + result[val].city + "</td>"
            html += '<td>' + result[val].nums + "</td>"
            html += '</tr>'

        }

        fs.readFile("./home/html/index.html", function (err, data) {
            console.log(err);
            let datas = data.toString();
            let formatHtml = datas.replace("{{result}}", html);
            res.write(formatHtml);
            res.end();

        })
    },
    show: async function(req,res) {
        let mysql = require("../../model/base");
        let date = req.query['date'];
        //console.log('date-----'+date);
        let sql = 'select * from number order by id desc limit 1 ';//取出最先的一条数据
        //console.log(res);
        let result= await mysql.query(sql,date,res);
        console.log(result);
        let html = '';
        for (let val in result) {
            html += '<tr>'
            html += '<td>' + result[val].city + "</td>"
            html += '<td>' + result[val].nums + "</td>"
            html += '</tr>'
        }

        fs.readFile("./home/html/index.html", function (err, data) {
            console.log(err);
            let datas = data.toString();
            let formatHtml = datas.replace("{{result}}", html);
            res.write(formatHtml);
            res.end();
        })
    }
}

module.exports = index;